import 'package:flutter/material.dart';
import 'package:flutter_swiper_null_safety/flutter_swiper_null_safety.dart';
import '../utils/adapt.dart';
import '../utils/request.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';

class MyBanner extends StatefulWidget {
  MyBanner({Key? key}) : super(key: key);

  @override
  _MyBannerState createState() => _MyBannerState();
}

class _MyBannerState extends State<MyBanner>
    with AutomaticKeepAliveClientMixin {
  List bannerList = [
    'http://p1.music.126.net/7jFZXkqWEG_NJf56BIYzhQ==/109951166366416460.jpg',
    'http://p1.music.126.net/7jFZXkqWEG_NJf56BIYzhQ==/109951166366416460.jpg'
  ];
  void _getBanner() async {
    EasyLoading.show(status: 'loading...');
    var res = await getHttp('/banner?type=2');
    EasyLoading.dismiss();
    var list = res['banners'];
    var arr = [];
    for (int i = 0; i < list.length; i++) {
      arr.add(list[i]['pic']);
    }
    this.setState(() {
      bannerList = arr;
    });
  }

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _getBanner();
  }

  @override
  Widget build(BuildContext context) {
    return AspectRatio(
      aspectRatio: 5 / 2,
      child: new Swiper(
        itemBuilder: (BuildContext context, int index) {
          return Container(
            padding: EdgeInsets.all(Adapt.px(6)),
            child: ClipRRect(
              borderRadius: BorderRadius.circular(Adapt.px(20)),
              child: new Image.network(
                bannerList[index] + '?param=600y240',
                fit: BoxFit.fill,
              ),
            ),
          );
        },
        itemCount: bannerList.length,
        pagination: new SwiperPagination(),
        // control: new SwiperControl(),
        autoplay: true,
      ),
    );
  }

  @override
  // TODO: implement wantKeepAlive
  bool get wantKeepAlive => true;
}
